﻿Imports System.Data
Imports System.Data.SqlClient
Public Class MarcaDAL
#Region "Incluir"
    Public Function Inserir(ByVal Descricao As String, ByVal CodStatus As Integer) As Integer
        Dim cmd = New SqlCommand
        Dim codigo As Integer
        Try

            cmd.CommandText = "insert into Vendas.Gen_Marca(mar_descricao,sta_codigo) values (@descricao,@status)"
            cmd.Parameters.Add(New SqlParameter("@descricao", SqlDbType.VarChar)).Value = Descricao
            cmd.Parameters.Add(New SqlParameter("@status", SqlDbType.Int)).Value = CodStatus
            codigo = objconexao.executaCmd(cmd)
        Catch ex As Exception
            Throw ex
        Finally
            cmd = Nothing
        End Try
        Return codigo
    End Function
#End Region

#Region "Alterar"
    Public Function Alterar(ByVal Descricao As String, ByVal Status As Integer, ByVal Codigo As Integer) As Integer
        Dim cmd = New SqlCommand
        Try

            cmd.CommandText = "update Vendas.Gen_Marca set mar_descricao=@descricao, sta_codigo=@status where mar_codigo=@codigo"
            cmd.Parameters.Add(New SqlParameter("@descricao", SqlDbType.VarChar)).Value = Descricao
            cmd.Parameters.Add(New SqlParameter("@codigo", SqlDbType.Int)).Value = codigo
            cmd.Parameters.Add(New SqlParameter("@status", SqlDbType.Int)).Value = Status
            codigo = objconexao.executaCmd(cmd)

        Catch ex As Exception
            Throw ex

        Finally
            cmd = Nothing

        End Try
        Return codigo
    End Function
#End Region

#Region "Deletar"
    Public Function Deletar(ByVal codigo As Integer) As Integer
        Dim cmd = New SqlCommand
        Dim ret As Integer

        Try

            cmd.CommandText = "update Vendas.Gen_Marca set  sta_codigo=3 where mar_codigo=@codigo"
            cmd.Parameters.Add(New SqlParameter("@codigo", SqlDbType.Int)).Value = codigo
            ret = objconexao.executaCmd(cmd)
        Catch ex As Exception
            Throw ex
        Finally
            cmd = Nothing
        End Try
        Return ret
    End Function
#End Region

#Region "Valida exclusão"
    Public Function ValidaExcluir(ByVal Codigo As Integer, Status As Integer) As Boolean
        Dim cmd = New SqlCommand
        Dim tab As DataTable
        Dim ret As Boolean = True
        Try
            If Status <> 1 Then
                cmd.CommandText = "select*from Vendas.Gen_Marca m inner join Vendas.Ofi_Carro c on m.mar_codigo=c.mar_codigo where m.sta_codigo <> 3 and m.mar_codigo=@codigo and c.sta_codigo <> 3 "
                cmd.Parameters.Add(New SqlParameter("@codigo", SqlDbType.Int)).Value = Codigo
                tab = objconexao.executaConsulta(cmd)
                If (tab.Rows.Count > 0) Then
                    ret = False
                End If
            End If
        Catch ex As Exception
            Throw ex
        Finally
            cmd = Nothing
        End Try
        Return ret
    End Function
#End Region

#Region "Buscar"
    Public Function buscar(ByVal Descricao As String, tipo As Integer) As DataTable
        Dim tab = New DataTable
        Dim cmd As SqlCommand
        Try
            cmd = New SqlCommand
            Select Case tipo
                Case 0
                    cmd.CommandText = "select * from vw_VenMarca where mar_codigo = @codigo"
                    cmd.Parameters.Add(New SqlParameter("@codigo", SqlDbType.Int)).Value = Val(Descricao)
                Case 1
                    cmd.CommandText = "select * from vw_VenMarca where mar_descricao COLLATE SQL_Latin1_General_CP1_CI_AI  like @descricao"
                    cmd.Parameters.Add(New SqlParameter("@descricao", SqlDbType.VarChar)).Value = "%" & Descricao & "%"
                Case 2
                    cmd.CommandText = "select * from vw_VenMarca where sta_descricao = @descricao"
                    cmd.Parameters.Add(New SqlParameter("@descricao", SqlDbType.VarChar)).Value = Descricao
                Case Else
                    cmd.CommandText = "select * from vw_VenMarca where sta_codigo < 3 order by mar_descricao"
            End Select

            tab = objconexao.executaConsulta(cmd)
            tab.DefaultView.AllowNew = False
        Catch ex As Exception
            Throw ex
        Finally
            cmd = Nothing
        End Try
        Return tab
    End Function
#End Region

End Class
